package com.woyao;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.woyao.core.model.ReferSummary;
import com.woyao.core.util.Common;
import com.woyao.core.util.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

import static com.woyao.WoyaoooApplication.snailview;

/**
 * Created by Administrator on 2016/9/19 0019.
 */

public class ReferAdapter extends RecyclerView.Adapter<ReferAdapter.ViewHolder>
{

    private LayoutInflater mInflater;

    private  List<ReferSummary> thedata = new ArrayList<ReferSummary>();

    private String Selection = null ;
    Context thecontext;

    public ReferAdapter(Context context, List<ReferSummary> data ) {
        thecontext = context;
        this.mInflater = LayoutInflater.from(context);

        thedata= data;

    }



    public String getSelection() {

        ArrayList<String> it = new ArrayList<String>();

        for(ReferSummary one: thedata){
            if ( one.getSelected()!= null && one.getSelected()  ){
                it.add(one.getId());
            }
        }

        return  Common.listToString(it );
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= mInflater.inflate(R.layout.refer_toggle_item,parent,false);
        //view.setBackgroundColor(Color.RED);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    /**
     * 数据的绑定显示
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final ViewHolder holder,final int position) {
        ReferSummary curData =  thedata.get(position);

        holder.theTitle.setText(curData.getTitle());
        holder.theDescription.setText(curData.getDescription());
        Picasso.with(thecontext)
                .load(curData.getSnailview())
                .into(holder.theSnailview);

        if ( curData.getSelected() ){
            holder.theCheck.setChecked(true);
            holder.theCheck.setEnabled(false);
        }

        holder.theCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    thedata.get(position).setSelected(isChecked);
            }
        });
    }

    @Override
    public int getItemCount() {
        return thedata.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView theTitle;
        public TextView theDescription;
        public CheckBox theCheck;
        public CircleImageView theSnailview;

        public ViewHolder(View view) {
            super(view);
            theTitle = (TextView) view.findViewById(R.id.contact_title);
            theDescription = (TextView) view.findViewById(R.id.contact_description);
            theCheck = (CheckBox) view.findViewById(R.id.contact_checkbox);
            theSnailview = (CircleImageView) view.findViewById(R.id.contact_snailview);
        }

    }

}
